/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.framework.util;

import org.apache.commons.text.RandomStringGenerator;

/**
 * 随机字符串生成工具
 *
 * @author liym
 * @since 2019-08-21 13:34 新建
 */
public final class RandomStringUtil {

    /**
     * 随机字符串器包含的字符范围：数字、大小写字母
     */
    private static final char[][] CHAR_PAIRS = {{'0', '9'}, {'A', 'Z'}, {'a', 'z'}};
    /**
     * 随机字符串器
     */
    private static final RandomStringGenerator GENERATOR;

    static {
        GENERATOR = new RandomStringGenerator.Builder()
                .withinRange(CHAR_PAIRS)
                // .selectFrom(',', ';', ':', '?', '*', '#', '!')
                .build();
    }

    private RandomStringUtil() {}

    /**
     * 生成随机字符串（固定长度）
     *
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String generate(final int length) {
        return GENERATOR.generate(length);
    }

    /**
     * 生成随机字符串（可变长度）
     *
     * @param minLengthInclusive 字符串最小长度（包含）
     * @param maxLengthInclusive 字符串最大长度（包含）
     * @return 随机字符串
     * @since 2019-11-12 10:43
     */
    public static String generate(final int minLengthInclusive, final int maxLengthInclusive) {
        return GENERATOR.generate(minLengthInclusive, maxLengthInclusive);
    }

    public static void main(String[] args) {
        System.out.println(generate(12));
        System.out.println(generate(8, 15));
    }

}
